package com.ruoyi.aqsc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.aqsc.domain.AShopManage;
import com.ruoyi.aqsc.service.AShopManageService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;



@RestController
@RequestMapping("/manage")
public class AShopManageController extends BaseController {

    @Autowired
    private AShopManageService service;

    /**
     * 分页查询车间信息
     * @param
     * @return
     */

    @GetMapping("/list")
    public TableDataInfo list(AShopManage aShopManage){
        startPage();
        LambdaQueryWrapper<AShopManage> wrapper =new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(aShopManage.getWorkshopName())) {
            wrapper.like(AShopManage::getWorkshopName, aShopManage.getWorkshopName());
        }
        List<AShopManage> list = service.list(wrapper);

        return getDataTable(list);
        }

    /**
     * 根据id查询车间信息
     * @param id
     * @return
     */
    @RequiresPermissions("aqsc:manage:update")
    @PostMapping("/findById/{id}")
    public AjaxResult findById(@PathVariable Integer id){
        AShopManage statuls = service.getById(id);
        System.out.println(statuls);
        return AjaxResult.success(statuls);
    }

    /**
     * 新增车间信息
     * @param aShopManage
     * @return
     */
    @RequiresPermissions("aqsc:manage:add")
    @PostMapping("/add")
    public AjaxResult add(AShopManage aShopManage){
//        boolean statuls = service.save(aShopManage);
        boolean statuls = service.saveOrUpdate(aShopManage);
        return AjaxResult.success(statuls);
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions("system:role:export")
    @DeleteMapping("/deleteByIds/{ids}")
    public AjaxResult delelteByIds(@PathVariable Integer[] ids){
        boolean statuls = service.removeByIds(Arrays.asList(ids));
        return AjaxResult.success(statuls);
    }

    // 文件上传
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<AShopManage> util = new ExcelUtil<AShopManage>(AShopManage.class);
        List<AShopManage> userList = util.importExcel(file.getInputStream());
        String message = service.importame(userList, updateSupport);
        return AjaxResult.success(message);
    }
}
